{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 67.[二进制求和](https://leetcode.cn/problems/add-binary/description/)\n",
    "\n",
    "|  Category  |  Difficulty   | Likes | Dislikes |\n",
    "| :--------: | :-----------: | :---: | :------: |\n",
    "| algorithms | Easy (53.78%) |  847  |    -     |\n",
    "\n",
    "<details open=\"\" style=\"color: rgb(246, 246, 244); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe WPC&quot;, &quot;Segoe UI&quot;, system-ui, Ubuntu, &quot;Droid Sans&quot;, sans-serif, &quot;Microsoft Yahei UI&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;\"><summary><strong>Tags</strong></summary><p style=\"margin-top: 0px; margin-bottom: 0.7em;\"><a href=\"https://leetcode.com/tag/math\" title=\"https://leetcode.com/tag/math\" style=\"color: var(--vscode-textLink-foreground); text-decoration: none;\"><code style=\"color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, &quot;SF Mono&quot;, Monaco, Menlo, Consolas, &quot;Ubuntu Mono&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;\">math</code></a><span>&nbsp;</span>|<span>&nbsp;</span><a href=\"https://leetcode.com/tag/string\" title=\"https://leetcode.com/tag/string\" style=\"color: var(--vscode-textLink-foreground); text-decoration: none;\"><code style=\"color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, &quot;SF Mono&quot;, Monaco, Menlo, Consolas, &quot;Ubuntu Mono&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;\">string</code></a></p></details>\n",
    "\n",
    "<details style=\"color: rgb(246, 246, 244); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe WPC&quot;, &quot;Segoe UI&quot;, system-ui, Ubuntu, &quot;Droid Sans&quot;, sans-serif, &quot;Microsoft Yahei UI&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;\"><summary><strong>Companies</strong></summary><p style=\"margin-top: 0px; margin-bottom: 0.7em;\"><code style=\"color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, &quot;SF Mono&quot;, Monaco, Menlo, Consolas, &quot;Ubuntu Mono&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;\"></code></p></details>\n",
    "\n",
    "给你两个二进制字符串，返回它们的和（用二进制表示）。\n",
    "\n",
    "输入为 **非空** 字符串且只包含数字 `1` 和 `0`。\n",
    "\n",
    " \n",
    "\n",
    "**示例 1:**\n",
    "\n",
    "```\n",
    "输入: a = \"11\", b = \"1\"\n",
    "输出: \"100\"\n",
    "```\n",
    "\n",
    "**示例 2:**\n",
    "\n",
    "```\n",
    "输入: a = \"1010\", b = \"1011\"\n",
    "输出: \"10101\"\n",
    "```\n",
    "\n",
    " \n",
    "\n",
    "**提示：**\n",
    "\n",
    "- 每个字符串仅由字符 `'0'` 或 `'1'` 组成。\n",
    "- `1 <= a.length, b.length <= 10^4`\n",
    "- 字符串如果不是 `\"0\"` ，就都不含前导零。****"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "vscode": {
     "languageId": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11+1 二进制相加的和为\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "/*\n",
    " * @lc app=leetcode.cn id=67 lang=csharp\n",
    " *\n",
    " * [67] 二进制求和\n",
    " */\n",
    "\n",
    "// @lc code=start\n",
    "public class Solution {\n",
    "    public string AddBinary(string a, string b) {\n",
    "        if(a == null || a.Length == 0) return b;\n",
    "        if(b == null || b.Length == 0) return a;\n",
    "        StringBuilder builder = new StringBuilder(10);\n",
    "        int i = a.Length - 1;\n",
    "        int j = b.Length - 1;\n",
    "        \n",
    "        int c = 0;  // 进位\n",
    "        while(i >= 0 || j >= 0) {\n",
    "            if(i >= 0) c += (char)a[i--] - '0';\n",
    "            if(j >= 0) c += (char)b[j--] - '0';\n",
    "            builder.Append(c % 2);\n",
    "            c >>= 1;\n",
    "        }        \n",
    "        string res = ReverseUsingXor(builder.ToString());\n",
    "        return c > 0 ? '1' + res : res;\n",
    "    }\n",
    "\n",
    "///用于反转字符串\n",
    "    public static string ReverseUsingXor(string str)\n",
    "    {\n",
    "        char[] arr = str.ToCharArray();\n",
    "        int l = str.Length - 1;\n",
    " \n",
    "    //交换值\n",
    "    for (int i = 0; i < l; i++, l--)\n",
    "    {\n",
    "        arr[i] ^= arr[l];\n",
    "        arr[l] ^= arr[i];\n",
    "        arr[i] ^= arr[l];\n",
    "    }\n",
    "    return new string(arr);\n",
    "    }\n",
    "}\n",
    "Solution solu = new Solution();\n",
    "Console.WriteLine(\"11+1 二进制相加的和为\");\n",
    "Console.WriteLine(solu.AddBinary(\"11\",\"1\"));"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
